package com.beuth.taskList.activities;

import java.util.Collections;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;

import com.beuth.taskList.R;
import com.beuth.taskList.activities.tools.DataIO;
import com.beuth.taskList.activities.tools.TaskComperator;
import com.beuth.taskList.model.SortType;
import com.beuth.taskList.model.Task;
import com.beuth.taskList.model.TaskModel;

public class TaskOverView extends Activity {
	
	private TaskModel model = TaskModel.getInstance();
	private DataIO dataIO = new DataIO();

	private ListView taskListView;
	private ArrayAdapter<Task> taskListViewAdapter;
	private Spinner spinnerSortType;
	
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
		Log.v("Model", ": On Create Called");
		super.onCreate(savedInstanceState);

		if (model.getTasks().size() <= 0) {
			try {
				dataIO.load(model.getTasks());
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
        
        setContentView(R.layout.activity_task_over_view);
        
        taskListView = (ListView) findViewById(R.id.listView_task_over_view);
        taskListViewAdapter = new ArrayAdapter<Task>(this, android.R.layout.simple_list_item_1, model.getTasks());
        taskListView.setAdapter(taskListViewAdapter);
        
        spinnerSortType = (Spinner) findViewById(R.id.spinner_sort_type);
	    spinnerSortType.setAdapter(new ArrayAdapter<SortType>(this, android.R.layout.simple_dropdown_item_1line, SortType.values()));
        
	    spinnerSortType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> aView, View view,	int arg2, long arg3) {
				SortType selectedSortType = (SortType) spinnerSortType.getSelectedItem();
    			sortTasks(selectedSortType);			
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
			}
		});
	    
        taskListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position,	long id) {
				Intent i = new Intent(view.getContext(), TaskCreateOrEdit.class);
				Bundle b = new Bundle();
				b.putInt("EditTaskNumber", position);
				i.putExtras(b);
				startActivity(i);				
			}
		});
    
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.task_over_view, menu);
        return true;
    }
    
    @Override
    protected void onResume() {
    	super.onResume();
    	//((ArrayAdapter<Task>) taskListView.getAdapter()).notifyDataSetChanged();
    }
    
    
    @Override
    protected void onPause() {
		Log.v("Model", ": On Pause Called");
    	super.onPause();
    	if (model.getTasks().size() > 0) {
    		//Log.v("Model", ": Save Tasks");
    		//dataIO.save(model.getTasks());
    	} 
    }

    	
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
      switch (item.getItemId()) {
      case R.id.createTask:
    	  Intent i = new Intent(this, TaskCreateOrEdit.class);
    	  startActivity(i);        			
        break;
      case R.id.showTasks:
    	  i = new Intent(this, TaskOverView.class);
    	  startActivity(i); 
        break;

      default:
        break;
      }

      return true;
    } 
    
    
	private void sortTasks(SortType selectedSortType) {
		Toast.makeText(this, "SelectedSortType: " + selectedSortType, Toast.LENGTH_LONG).show();
	
		Collections.sort(model.getTasks(), new TaskComperator(selectedSortType));
		
		//Dem Adapter bescheid geben, dass was passiert ist
		taskListViewAdapter.notifyDataSetChanged();
	}

}
